package com.jch.netty_client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.StandardCharsets;

public class DemoChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        System.out.println("channelId：" + ch.id());

        // 保持与服务器同样的编解码方式，半包沾包处理，收发数据处理。
        // 基于换行符 "\r\n"
        ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
        // 字符串解码器
        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
        // 字符串编码器
        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
        // 自定义处理器
        ch.pipeline().addLast(new DemoClientHandler());
    }
}
